from django.shortcuts import render

# Create your views here.

from rest_framework.views import APIView
from rest_framework.response import Response
from myapp.models import Kind,Books,Png
from myapp.serializers import KindSer,BooksSer,PngSer



# 获取所有的种类：
class CateView(APIView):
    def get(self,request):
        page = int(request.query_params.get('page'))
        pagesize = int(request.query_params.get('pagesize'))
#         获取所有的种类：
        kind_data = Kind.objects.all()[((page-1)*pagesize):(page*pagesize)]
#         序列化：
        ser = KindSer(kind_data,many=True)
#         返回：
        return Response({'dat':ser.data,'msg':'获取成功','code':200,'count_total':Kind.objects.all().count()})

class CateViewl(APIView):
    def get(self, request):
        kind_data = Kind.objects.all()
        ser = KindSer(kind_data, many=True)
        return Response(ser.data)


# 根据id进行删除：
class DelVIew(APIView):
    def delete(self,request,id):
#         根据id删除：
        try:
            Kind.objects.get(id=id).delete()
        except Exception as e:
            print(e)
            return Response({'msg':'删除失败！','code':404})
        return Response({'msg':'删除成功','code':200})




# 获取对应的图书信息：
class TuShuView(APIView):
    def get(self,request):
        id = request.query_params.get('id')
        try:
#             通过id找到单个种类
            kind_name = Kind.objects.get(id=id)
        except Exception as e:
            print(e)
            return Response({'msg':'数据不存在！','code':404})
        books_data = kind_name.books_set.all()
#         进行序列化：
        ser = BooksSer(books_data,many=True)
#         返回：
        return Response(ser.data)



# 添加数据：
class AddView(APIView):
    def post(self,request):
#         获取前端数据：
        name = request.data.get('name')
        price = request.data.get('price')
        num = request.data.get('num')
        cate = request.data.get('cate')

        try:
            Books.objects.create(
                name=name,
                price=price,
                num=num,
                cate_id=cate
            )
        except Exception as e:
            print(e)
            return Response({'msg':'添加失败！','code':404})
        return Response({'msg':'添加成功','code':200})



class DelView(APIView):
    def delete(self,request):
        id = request.data.get('id')
        try:
            Books.objects.get(id=id).delete()
        except Exception as e:
            print(e)
            return Response({'msg':'数据不存在！','code':404})
        return Response({'msg':'删除成功','code':200})



# 进行修改：
class UpdateView(APIView):
    def get(self,request,id):
        try:
            book_data=Books.objects.get(id=id)
        except Exception as e:
            print(e)
            return Response({'msg':'数据不存在！','code':404})
        ser = BooksSer(book_data)
        return Response(ser.data)
    def put(self,request,id):
        try:
            bood_data = Books.objects.get(id=id)
        except Exception as e:
            print(e)
            return Response({'msg':'数据不存在！','code':404})

        ser = BooksSer(data=request.data,instance=bood_data)

        if not ser.is_valid(raise_exception=True):
            return Response({'msg':'修改失败！','code':404})
        ser.save()
        return Response({'msg':'修改成功','code':200})


# 获取图书的数据：
class TuAllView(APIView):
    def get(self,request):
        book_data = Png.objects.all()
        ser = PngSer(book_data,many=True)
        return Response(ser.data)






#         print(id)
#         name = request.data.get('name')
#         price = request.data.get('price')
#         num = request.data.get('num')
#         cate = request.data.get('cate')
#         print(name)
#         print(price)
#         print(num)
#         print(cate)
# #       找到该数据：
#         try:
#             book_data=Books.objects.get(id=id)
#             book_data.name=name,
#             book_data.price=price,
#             book_data.num=num,
#             book_data.cate_id=cate
#             book_data.save()
#         except Exception as e:
#             print(e)
#             return Response({'msg':'修改失败！','code':404})
#         return Response({'msg':'修改成功','code':200})



# 七牛云：
# access_key = 'ZdK2iJDBszREVaZ_CXTgBRdJ_p00X1NsCL8CsOOV'
# secret_key = '5mJjAR9wUJ72TLRDP6aAEam5Yj9wuxSgLn21OXeW'

# from qiniu import Auth
# # class QIniuView(APIView):
# #     def get(self,request):
# #         q = Auth(access_key,secret_key)
# # #         存储地址：
# #         kong = 'h2112a1'
# # #         生成token：
# #         token = q.upload_token(kong)
# # #         返回数据：
# #         return Response({'msg':'获取成功','token':token,'code':200})



# 上传到七牛云：

access_key = 'ZdK2iJDBszREVaZ_CXTgBRdJ_p00X1NsCL8CsOOV'
secret_key = '5mJjAR9wUJ72TLRDP6aAEam5Yj9wuxSgLn21OXeW'

from qiniu import Auth
class QIniuView(APIView):
    def get(self,request):
        q = Auth(access_key,secret_key)
#         上传空间：
        kong = 'h2112a1'
#         生成token：
        token = q.upload_token(kong)
#         返回：
        return Response({'msg':'获取数据成功','code':200,'token':token})



from project11 import settings
# 添加到静态：
class JingtaiView(APIView):
    def post(self,request):
#         获取前端文件：
        files = request.FILES.get('file')
#         存到文件夹中
        files_dir = settings.STATICFILES_DIRS[0]
#       生成路径
        files_name = files_dir + files.name
#     打开文件：
        with open(files_name,'wb') as f:
            f.write(files.file.read())

        return Response({'msg':'获取成功','code':200,'files_dir':files_dir,'files_name':files.name})






